package copyengine.utils.delayEffect.logic
{
	import copyengine.utils.delayEffect.effect.ICEDelayEffect;

	import flash.display.DisplayObjectContainer;

	public final class CEDelayEffectExecuteWarp
	{
		private var manger:CEDelayEffectExecuteWarpManger;

		/**
		 *DelayEffect流程播放结束后的CallBack函数
		 * 流程结束不代表整个DelayEffect结束，比如像金币掉落动画，金币掉落在地上后认为Flow结束
		 * 但是用户点击，或者时间片到时会触发另外一个动画，并且有数据的交换，当这些都完成时候才认为
		 * 当前DelayEffect播放结束
		 */
		private var flowFinishCallBack:Function;

		private var eachStepDelayEffectVector:Vector.<ICEDelayEffect>;

		/**
		 *记录当前的Flow执行到的步骤
		 */
		private var currentFlowExecuteStep:int;

		/**
		 * 记录当前Effect完成的个数
		 */
		private var currentEffectFinishNum:int;

		public function CEDelayEffectExecuteWarp()
		{
			eachStepDelayEffectVector = new Vector.<ICEDelayEffect>();
		}

		public function initialize(_flowFinishCallBack:Function,
								   _warpManger:CEDelayEffectExecuteWarpManger):void
		{
			flowFinishCallBack = _flowFinishCallBack;
			manger = _warpManger;
		}

		public function setExecuteLayer(_sceneUILayer:DisplayObjectContainer,
										_sceneLayer:DisplayObjectContainer,
										_globalUILayer:DisplayObjectContainer):void
		{
			for each (var effect:ICEDelayEffect in eachStepDelayEffectVector)
			{
				effect.setLayer(_sceneUILayer, _sceneLayer, _globalUILayer);
			}
		}

		public function dispose():void
		{
			while (eachStepDelayEffectVector.length > 0)
			{
				eachStepDelayEffectVector.pop().dispose();
			}
			eachStepDelayEffectVector = null;
		}

		public function finishAllDelayEffectImmediately():void
		{
			tryToCallBackFlowFinish();
			for each (var effect:ICEDelayEffect in eachStepDelayEffectVector)
			{
				if (!effect.isExecuteFinish())
				{
					effect.finishExecuteImmediately();
				}
			}
		}

		public function addEffect(_effect:ICEDelayEffect):void
		{
			eachStepDelayEffectVector.push(_effect);
		}

		public function execute():void
		{
			currentFlowExecuteStep = -1;
			tryToPlayNextStepEffect();
		}

		private function tryToPlayNextStepEffect():void
		{
			currentFlowExecuteStep++;
			if (currentFlowExecuteStep < eachStepDelayEffectVector.length)
			{
				var effect:ICEDelayEffect = eachStepDelayEffectVector[currentFlowExecuteStep];
				effect.execute(tryToPlayNextStepEffect, onEffectExecuteFinish);
			}
			//else 所有Effect播放完毕
			else
			{
				//当所有子动画播放完毕后向上抛出Callback
				tryToCallBackFlowFinish();
			}
		}

		private function onEffectExecuteFinish():void
		{
			currentEffectFinishNum++;
			if (currentEffectFinishNum >= eachStepDelayEffectVector.length)
			{
				onWarpExecuteFinish();
			}
		}

		private function tryToCallBackFlowFinish():void
		{
			flowFinishCallBack && flowFinishCallBack();
			flowFinishCallBack = null;
			for each (var effect:ICEDelayEffect in eachStepDelayEffectVector)
			{
				effect.onFlowExecuteFinish();
			}
		}

		private function onWarpExecuteFinish():void
		{
			manger.removeExecuteWarp(this);
		}

	}
}
